package com.repair.servlet.yonghuguanli;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.repair.dao.Module.impl.ModuleDao;
import com.repair.dao.Operator.impl.OperatorDao;
import com.repair.domain.Module;
import com.repair.domain.Operator;
import com.repair.domain.Rule;
import com.repair.service.Module.ModuleService;
import com.repair.service.Operator.OperatorService;
import com.repair.service.Rule.RuleService;

public class AddopServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7080999345635550130L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		RuleService rs = new RuleService();
		
		String op  = req.getParameter("op");
		if(op == null){
			List<Rule> rule = new ArrayList<Rule>();
			rule =rs.list();
			req.setAttribute("rule", rule);
			
			RequestDispatcher rd = req.getRequestDispatcher("addoperator.jsp");
			rd.forward(req, resp);
		}
		if ("add".equals(op) && op != null) {
			int operator = 0;
			int ruleid =0;
			String opid = req.getParameter("operatorid");
			String opname = req.getParameter("operatorname");
			String oppwd = req.getParameter("operatorpwd");
			String rid=req.getParameter("ruleid");
			ruleid =Integer.parseInt(rid);
			OperatorDao service = new OperatorDao();
			Operator obj = new Operator();
			obj.setOperatorid(opid);
			obj.setOperatorname(opname);
			obj.setOperatorpwd(oppwd);
			obj.setRuleid(ruleid);
			service.saveOperator(obj);
			
			
			RequestDispatcher rd = req.getRequestDispatcher("yonghuguanli?op=view");
			rd.forward(req, resp);
		}

		
	}

}
